package com.example.cloudpracticeplatform.controller;

import com.example.cloudpracticeplatform.common.Response;
import com.example.cloudpracticeplatform.entity.ExperimentContainer;
import com.example.cloudpracticeplatform.service.DockerService;
import com.example.cloudpracticeplatform.service.ExperimentContainersService;
import com.example.cloudpracticeplatform.utils.DockerUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/docker")
@CrossOrigin
public class DockerController {
    @Resource
    private DockerService dockerService;
    @GetMapping("/info")
    public String getDockerInfo() {
        try {
            DockerUtils.dockerInfo();
            return "Docker info retrieved successfully";
        } catch (Exception e) {
            return "Failed to retrieve Docker info: " + e.getMessage();
        }
    }
    
    @PostMapping("/createAndStart/container")
    public Response<ExperimentContainer> createContainer(@RequestParam Integer studentId, @RequestParam Integer courseId) {
        return dockerService.createAndStartContainer(studentId,courseId);
    }

    @PostMapping("/create/image/{courseId}")
    public Response createImage(@PathVariable Integer courseId) {
        try {
            return dockerService.createImage(courseId);
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }
    
    @PostMapping("/stopAndDel/container")
    public Response stopAndDelContainer(@RequestParam Integer studentId, @RequestParam Integer courseId){
        return dockerService.stopAndDelContainer(studentId,courseId);
    }
    
    @PostMapping("/rebuild/container")
    public Response rebuildContainer(@RequestParam Integer studentId, @RequestParam Integer courseId){
        return dockerService.rebuildContainer(studentId,courseId);
    }
    
    
}